/**
 * @file router.h
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2024-07-19
 * 
 * @copyright Copyright (c) 2024
 * 
 */

#include "sampleRouter.h"
#include "basic/trace.h"

namespace wheels
{

int SampleRouter::Register(const char *path, HttpMethod method, Handler handler)
{
    if (method == HttpMethod::NUMBER)
    {
        trace_err("method is invalid");
        return -1;
    }

    auto &paths = m_handlers.at((int)method);
    auto r = paths.emplace(path, handler);
    if (r.second == false)
    {
        trace_err("has register: %d,%s\n", method, path);
        return -1;
    }
    return 0;
}

auto SampleRouter::GetHandler(const char *path, HttpMethod method) -> Handler
{
    if (method == HttpMethod::NUMBER)
    {
        trace_err("method is invalid");
        return Handler();
    }

    auto &paths = m_handlers.at((int)method);
    auto r = paths.find(path);
    if (r != paths.end())
    {
        return r->second;
    }
    return Handler();
}

} // namespace wheel
